import com.demo.mqtt.common.dto.MqttDto;
import com.demo.mqtt.controller.MqttGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: liuYunKai
 * @create: 2021-03-24 10:58
 * @description mqtt发布消息
 */
@RestController
@RequestMapping(value = "/mqtt")
public class MqttController {

    @Autowired
    private MqttGateway mqttGateway;

    /**
     * @param  topic 主题 payload：内容
     * @return send message
     */
    @PostMapping("/send")
    public String  send(MqttDto dto) {
        // 发送消息到指定topic
        try {
            String topic = dto.getTopic();
            String message = dto.getPayload();
            mqttGateway.sendToMqtt(topic, message);
            return "OK, send message :"+message;
        } catch (Exception e) {
            e.printStackTrace();
            return "error, error message :"+ e.getMessage();
        }
    }
}
